import java.util.ArrayList;
import java.util.Arrays;

/**
 * @program: LeetCode
 * @description: LeetCode : 56. 合并区间
 * @author: WXY
 * @create: 2023-01-24 10:08
 * @Version 1.0
 **/
public class Num056_merge {
    public static int[][] merge(int[][] intervals) {

        Arrays.sort(intervals, (a, b) -> (a[0] - b[0]));
        ArrayList<int[]> list = new ArrayList<>();
        int left = intervals[0][0];
        int rihgt = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] <= rihgt) {
                rihgt = Math.max(rihgt, intervals[i][1]);
            } else {
                list.add(new int[]{left, rihgt});
                left = intervals[i][0];
                rihgt = intervals[i][1];
            }
        }
        list.add(new int[]{left, rihgt});
        return list.toArray(new int[list.size()][]);
    }

    public static void main(String[] args) {
        int[][] arr = {{1, 4}, {2, 3}};
        int[][] merge = merge(arr);
        System.out.println(merge);

    }
}
